home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / Winter Shell 1.0d2 / Source / Libraries / TimeLib / TimeLib.c next >
Encoding:
C/C++ Source or Header  |  1993-04-07  |  1.9 KB  |  65 lines  |  [TEXT/KAHL]

  1. /* Functions related to time.
  2.  
  3.     Revision History:
  4.     
  5.     91/11/14 AIH
  6.     - Fixed up the delay function so it chooses between allowing background
  7.     tasks for long delays or calling the Delay trap for short delays.
  8.     
  9.     91/06/13 AIH
  10.     - Added function to delay for a specified number of ticks
  11.     
  12.     91/03/23 AIH
  13.     - Merged TicksLib into TimeLib
  14.     - Changed TicksType and TimeType to size_t
  15.     - Removed #define which made TickCount be the global variable Ticks,
  16.     for compatability with AUX
  17.     - Changed TimeStartupSame in to a more general function
  18.     
  19.     91/01/31 Ari Halberstadt (AIH)
  20.     - Created this file by moving a couple of functions out of MacLib.c */
  21.     
  22. #include "TimeLib.h"
  23.  
  24. /* return time (in seconds) at which the computer was started up */
  25. TimeType TimeStartup(void)
  26. {
  27.     TimeType secs; /* current time */
  28.     
  29.     GetDateTime(&secs);
  30.     return(secs - (TickCount() / TICKS_SEC));
  31. }
  32.  
  33. /* true if t2 is within ±thresh seconds of t1 */
  34. Boolean TimeSame(TimeType t1, TimeType t2, TimeType thresh)
  35. {
  36.     return(t1 - thresh <= t2 && t2 <= t1 + thresh);
  37. }
  38.  
  39. /* Suspend execution for the specified number of ticks. To the calling
  40.     process this is identical to using Delay, but it allows other
  41.     processes to receive time. Using WNE is too slow, resulting in delays
  42.     in excess of small requested periods, so EventAvail is used instead.
  43.     Even using EventAvail, short delays could take longer than expected,
  44.     so we use the "Delay" function for very short delays. The actual delay
  45.     could be larger than the requested delay due to unpredictable system
  46.     usage. If you need accurate timing, use the Time Manager. */
  47. void TimeDelay(TicksType delay)
  48. {
  49.     const TicksType small = TICKS_SEC / 2;
  50.     EventRecord event;/* dummy event record */
  51.     TicksType start;    /* time when we started */
  52.     TicksType stop;    /* time when we stoped */
  53.     
  54.     start = TickCount();
  55.     if (delay < small)
  56.         Delay(delay, (long *) &stop);
  57.     else {
  58.         while (TickCount() - start < delay)
  59.             (void) EventAvail(0, &event);
  60.         stop = TickCount();
  61.     }
  62.     ensure(stop - start >= delay);
  63. }
  64.  
  65.